package com.lebang.component;


import com.lebang.mapper.buss.multiword.MultiwordMapper;
import com.lebang.mapper.buss.multiword.MultiwordSpeechMapper;
import com.lebang.mapper.buss.multiword.MultiwordSpeechPlanMapper;
import com.lebang.model.vo.buss.multiword.MultiwordSpeech;
import com.lebang.model.vo.buss.multiword.MultiwordSpeechPlan;
import com.lebang.service.TaskInRedisService;
import com.lebangweb.common.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

import static com.lebang.service.task.impl.MultiwordServiceImpl.TASK_MULTIWORD_AMOUNT;

/**
 * @Author: hwq
 * @Date: 2019/6/5 17:28
 * @Version 1.0
 */
@Component
public class MultiwordComponent {
    @Autowired
    private MultiwordSpeechMapper multiwordSpeechMapper;
    @Autowired
    private MultiwordSpeechPlanMapper multiwordSpeechPlanMapper;
    @Autowired
    private MultiwordMapper multiwordMapper;
    @Autowired
    private TaskInRedisService taskInRedisService;

    /**获取关键词每日量*/
    public int getAmountById(Long id) {
        List<MultiwordSpeech> mslist = multiwordSpeechMapper.getMultiwordSpeech(id);
        int i = 0;
        for (MultiwordSpeech item : mslist) {
            Integer multiwordSpeech = taskInRedisService.getTaskAmount(TASK_MULTIWORD_AMOUNT, item.getId().toString());
            if (multiwordSpeech != null) {
                i = i + multiwordSpeech;
            }
        }
        return i;
    }

    /**根据多词任务id获取随机的关键字id*/
    public Long findSumAmount(Long multiwordId) {
        List<MultiwordSpeech> mslist = multiwordSpeechMapper.getMultiwordSpeech(multiwordId);
        MultiwordSpeech multiwordSpeech = this.getMultiwordSpeechForList(mslist);
        if (multiwordSpeech != null) {
            return multiwordSpeech.getId();
        } else {
            return 0L;
        }

    }


    /**在redis中获取多词任务中随机一个还有每日量的任务对象*/
    private MultiwordSpeech getMultiwordSpeechForList(List<MultiwordSpeech> mslist) {
        List<Integer> integerList = new ArrayList<>(1);
        int ind = 0;
        for (MultiwordSpeech it : mslist) {
            if (it.getHasPlan() == 1) {
                String datestr = DateUtil.dateToString(new Date(), "yyyyMMdd");
                MultiwordSpeechPlan multiwordSpeechPlan = multiwordSpeechPlanMapper.getmultiwordSpeech(it.getId(), datestr);
                if (multiwordSpeechPlan == null) {
                    integerList.add(ind);
                }
            }
            ind++;
        }
        for (int it : integerList) {
            mslist.remove(it);
        }
        Random random = new Random();
        Set<Integer> olist = new HashSet<>(1);
        int index = 0;
        int size = mslist.size();
        while (true) {
            if (index >= size) {
                return null;
            }
            index++;
            int i2 = random.nextInt(mslist.size());
            if (!olist.contains(Integer.parseInt(mslist.get(i2).getId().toString()))) {

                Integer multiwordSpeech =  taskInRedisService.getTaskAmount(TASK_MULTIWORD_AMOUNT,  mslist.get(i2).getId().toString());
                if (multiwordSpeech == null) {
                    return null;
                }
                olist.add(Integer.parseInt(mslist.get(i2).getId().toString()));
                if (multiwordSpeech > 0) {
                    return this.multiwordSpeechMapper.selectByPrimaryKey(mslist.get(i2).getId());
                } else {
                    mslist.remove(i2);
                }
            }
        }
    }

    public List<Map<String, Object>> selectMultiwordById(Long id) {
        return multiwordMapper.selectMultiwordById(id);
    }

    public void updatetodaytotal() {
        multiwordSpeechMapper.updatetodaytotal();
    }

}
